package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.operations.reports.forms.BinningReportForm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Oct 19, 2007
 * Time: 4:05:47 PM
 * To change this template use File | Settings | File Templates.
 */
public class BinningReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/binningReportAction.do";
    public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        String action = request.getParameter( "action" );

        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        try
        {
            ArrayList processList   = new ArrayList();
            ArrayList productList   = null;
            ArrayList vendorList    = null;

            String reportType       = null;
            String processType      = null;

            if ( action.equals( "binningReport" ) )
            {
                try
                {
                    BinningReportForm binningReportForm = (BinningReportForm)form;
                    processType = binningReportForm.getPsType();

                    if( processType == null || "0".equalsIgnoreCase ( processType ) )
                    {
                       //productList = NewProductDAO.loadAllBaseProducts( null, true, true );
                       productList = new ArrayList(); 
                       vendorList = new ArrayList();
                       vendorList.add( new LabelValueBean( "All Vendors", "0") ); 
                       //vendorList = VendorDAO.getVendorNamesByProcessByUserType( null, "all", user );
                    }
                    else
                    {

                       productList = NewProductDAO.loadPartNumbers( null, 0, processType, true, true, true, 0 );
                       if ("FT".equalsIgnoreCase(processType))
                       {
                           processType = "TEST";
                       }
                       vendorList = VendorDAO.getVendorNamesByProcessByUserType( null, processType, user );
                    }


                }
                catch( Exception e )
                {
                    processList     = new ArrayList();
                    productList     = new ArrayList();
                    vendorList      = new ArrayList();

                    e.printStackTrace();
                }
                request.setAttribute( "processList", loadProcessTypes() );
                request.setAttribute ( "vendorList" , vendorList );
                request.setAttribute( "productList", productList );
                request.setAttribute( "categoryList", RelaseDAO.getRequestTypes() );
                request.setAttribute ( "statusList" , NewProductDAO.loadStatusList() );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        if( action.equals( "binningReport" ) )
        {
            return( mapping.findForward( "binningReport" ) ) ;
        }
        else
        return null;
    }

    private ArrayList loadProcessTypes()
    {
        ArrayList processList = new ArrayList();

        processList.add( new LabelValueBean( "Select Process", "0") );
        processList.add( new LabelValueBean( "Sort", "Sort") );
        processList.add( new LabelValueBean( "Test", "FT" ) );

        return processList;
    }
}
